package com.api.service.impl;

import com.api.mapper.UserMapper;
import com.api.pojo.User;
import com.api.service.UserService;
import com.baomidou.dynamic.datasource.annotation.DS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public int createUser(User user){
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String creation_date = dateFormat.format(System.currentTimeMillis());
        user.setCreation_date(creation_date);
        return userMapper.addUser(user);
    }

    @Override
    public int updateUser(int id, User user){
        user.setUserid(id);
        return userMapper.updateUser(user);
    }

    @Override
    public int deleteUser(int id){
        return userMapper.deleteUser(id);
    }

    @Override
    @DS("slave")
    public User queryUserById(int id){
        return userMapper.queryUserById(id);
    }

    @Override
    @DS("slave")
    public List<User> queryUserList(int pageNow, int pageSize){
        int pageNum = (pageNow-1)*pageSize;
        return userMapper.queryUserList(pageNum,pageSize);
    }

    @Override
    @DS("slave")
    public int login(String username, String password){
        return  userMapper.login(username,password);
    }

}
